package com.homegoods.videomodule.ui.fragments;

import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.resource.SpacesItemDecoration;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.util.Utils;
import com.homegoods.videomodule.R;
import com.homegoods.videomodule.item.FreedomCommunityItem;
import com.homegoods.videomodule.presenter.FreedomCommunityPresenter;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.base.KRefreshListActivity;
import com.kedll.library.databinding.GeneralIncludeActionbarAndListBinding;
import com.kedll.library.fragment.KRefreshListFragment;
import com.kedll.library.util.MyUtils;

/**
 * @author QiQi
 * @description 免费社区
 */
@Route(path = ARouterPath.PATH_COMMUNITY_FREEDOM)
public class FreedomCommunityFragment extends KRefreshListFragment<GeneralIncludeActionbarAndListBinding, FreedomCommunityPresenter> {

    @Override
    public int getLayout() {
        return R.layout.general_include_actionbar_and_list;
    }

    @Override
    protected void init() {
        super.init();
        int padding = MyUtils.dp2px(8);
        list.setPadding(padding, padding, padding, 0);
        list.addItemDecoration(new SpacesItemDecoration(2, 8));
    }

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        super.initEvent(savedInstanceState);
        adapter.setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<FreedomCommunityItem>) (v, iItem) -> {
            if (Build.VERSION.SDK_INT < 16) {
                ARouter.getInstance()
                        .build(ARouterPath.PATH_SHOT_VIDEO)
                        .withObject("data", iItem.getData())
                        .navigation();
            } else {

                ImageView imageView = v.findViewById(R.id.thumb);
                int[] loc = new int[2];
                imageView.getLocationInWindow(loc);

                ActivityOptionsCompat compat = ActivityOptionsCompat.makeScaleUpAnimation(imageView, loc[0], loc[1], 0, 0);

                ARouter.getInstance()
                        .build(ARouterPath.PATH_SHOT_VIDEO)
                        .withObject("data", iItem.getData())
                        .withOptionsCompat(compat)
                        .navigation(getContext());
            }
        });
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return Utils.getGridLayoutManager(getContext(), adapter, 2);
    }


    @Override
    protected FreedomCommunityPresenter getPresenter() {
        return new FreedomCommunityPresenter(this);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
